clear all 
set more off 
set maxvar 15000 
clear matrix


	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1
    
    foreach var of varlist log_father_baseline rank_father_baseline  {
        preserve
        estimates clear 

        * Dependent variables
            if "`var'"=="log_father_baseline" local dep "log_son_baseline" 
            if "`var'"=="rank_father_baseline" local dep "rank_son_baseline" 
            
            
            forval i=1(1)3 {
                gen est_`i'=. 
                gen est_lb_`i' =.
                gen est_ub_`i' =.
            }
            
            levelsof decade, local(decades) 
            
            *1. Men
            foreach x of local decades {
            eststo : quietly reg `dep' `var'  if sex==1 & decade==`x' [pw=wgt_sex_race], robust 
                replace est_1 = _b[`var'] if decade==`x'
                replace est_ub_1 = _b[`var']+1.96*_se[`var'] if decade==`x'
                replace est_lb_1 = _b[`var']-1.96*_se[`var'] if decade==`x'
            }

            *2. Women
            foreach x of local decades {
            eststo : quietly reg `dep' `var' if sex==2 & decade==`x' [pw=wgt_sex_race], robust 
                replace est_2 = _b[`var'] if decade==`x'
                replace est_ub_2 = _b[`var']+1.96*_se[`var'] if decade==`x'
                replace est_lb_2 = _b[`var']-1.96*_se[`var'] if decade==`x'
            }
            
        * Figure
            bysort decade: keep if _n==1
            keep decade est_*
            
            reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
            replace decade= decade+2 if estimate==2 

            if "`var'"=="log_father_baseline" {
                local title "IGE"
                local ub "1.25"
                local lb "0"
                local skip "0.25"
            }

            if "`var'"=="rank_father_baseline" {
                local title "Rank"
                local ub "0.45"
                local lb "0.15"
                local skip "0.1"
            }
                
            #delimit ;
            twoway (scatter est_ decade if estimate==1,  msymbol(circle_hollow) mcolor(navy) msize(medium) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(navy) lwidth(0.5))
                   (scatter est_ decade if estimate==2,  msymbol(triangle_hollow) mcolor(pink) msize(small) ) 
                   (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(pink) lwidth(0.5) ),
            xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
            ylabel(`lb'(`skip')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
            legend(on ring(0) pos(8) rows(1) order(1 "Men" 3 "Women" ))
            xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
            #delimit cr
	graph export "$Mydirectory2/main_figures_tables/figure4_`title'.pdf", as(pdf) replace
      
        restore
    }